package com.up.upzl.biz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.up.upzl.biz.domain.ShopCondition;
import com.up.upzl.biz.service.ShopConditionService;

/**
 * 商铺条件筛选控制器
 * 
 * @author joker
 *
 */
@Controller
@RequestMapping("shopCondition")
public class ShopConditionController {

	@Autowired
	private ShopConditionService shopConditionService;

	/**
	 * 价格筛选列表
	 * 
	 * @param page
	 *            分页信息
	 * @return 价格筛选分页数据
	 */
	@GetMapping("list")
	public String AppList(Pageable page, Model model) {
		page = new PageRequest(page.getPageNumber(), page.getPageSize(), new Sort(Direction.DESC, "sort"));
		Page<ShopCondition> list = shopConditionService.findAll(page);
		model.addAttribute("pageList", list);
		return "shopCondition/list";
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("addPage")
	public String add(Model model) {
		return "shopCondition/add";
	}

	/**
	 * 保存对象
	 */
	@PostMapping("add")
	public @ResponseBody ShopCondition save(ShopCondition shopCondition) {
		return shopConditionService.save(shopCondition);
	}

	/**
	 * 跳转到更新页面
	 */
	@GetMapping({ "updatePage", "detail" })
	public String update(Long id, Model model) {
		ShopCondition shopCondition = shopConditionService.findOne(id);
		model.addAttribute("entity", shopCondition);
		return "shopCondition/update";
	}

	/**
	 * 更新
	 */
	@PostMapping("update")
	public @ResponseBody ShopCondition update(ShopCondition shopCondition) {
		Assert.notNull(shopCondition.getId(), "Id can`t be null");
		return shopConditionService.save(shopCondition);
	}

}
